package com.ohimy.action;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.ohimy.entity.User;
import com.ohimy.entity.UserDetail;
import com.ohimy.service.impl.UserService;

@Controller("userAction")
public class UserAction extends BaseAction {

	private static final long serialVersionUID = 8639086233445562469L;

	@Resource(name = "userService")
	private UserService userService;

	private User user = new User();
	private UserDetail userDetail = new UserDetail();

	private String[] opt;

	public String save() throws Exception {
		user.setCreated(new Date());
		user.setHeader("default.jpg");
		userService.createUser(user);
		return null;
	}

	public String delete() throws Exception {
		userService.deleteUser(id);
		return null;
	}

	/**
	 * 用户登录
	 * 
	 * @return
	 * @throws Exception
	 */
	public String login() throws Exception {
		if (userService.isUser(user)) {
			System.out.println("1");
			userService.insertVisitTime(user);
			System.out.println("2");
			User member = userService.findUserIdByEntity(user);
			System.out.println("3");
			super.session.put("user", member);
			System.out.println("5");
			return "login_sus";
		} else {
			super.request.put("error", "error");
			return ERROR;
		}
	}

	/**
	 * 基本信息修改
	 * 
	 * @return
	 * @throws Exception
	 */
	public String infomodify() throws Exception {
		System.out.println(user.getId());
		String opts = opt[0] + "." + opt[1] + "." + opt[2];
		user.getUserDetail().setHometown(opts);
		System.out.println(user.getUserDetail().getHometown());
		userService.updateUser(user);
		// result = "/setting/";
		return null;// "ohimy";
	}

	/**
	 * 用户退出
	 * 
	 * @return
	 * @throws Exception
	 */
	public String logout() throws Exception {
		super.session.remove("user");
		result = "/";
		return "ohimy";
	}

	public String reg() throws Exception {
		result = "/WEB-INF/site/user/reg.jsp";
		return "ohimy";
	}

	// seter getter ////////////////
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserDetail getUserDetail() {
		return userDetail;
	}

	public void setUserDetail(UserDetail userDetail) {
		this.userDetail = userDetail;
	}

	public String[] getOpt() {
		return opt;
	}

	public void setOpt(String[] opt) {
		this.opt = opt;
	}

}